package com.zk.mybatisplus.test.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zk.mybatisplus.test.model.UserInfoDO;
import com.zk.mybatisplus.test.service.UserInfoService;
import com.zk.mybatisplus.test.service.UserInfoService2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author kuN
 * @date 21:14
 */

@Api(tags = "用户信息")
@RestController
@RequiredArgsConstructor
@RequestMapping(path = "/api/demo/v1/userinfo")
public class UserInfoController {
    private final UserInfoService userInfoService;
    private final UserInfoService2 userInfoService2;

    @ApiOperation("根据id获取用户")
    @GetMapping("/{id}")
    HttpEntity<UserInfoDO> getUserById(@PathVariable("id") Long userId) {
///        userInfoService2.getById(userId);
        return ResponseEntity.ok(userInfoService.getUserById(userId));
    }

    @ApiOperation("根据参数获取用户")
    @GetMapping
    HttpEntity<UserInfoDO> getUserByParam(@RequestParam("name") String name) {
///        userInfoService2.getOne(Wrappers.<UserInfoDO>lambdaQuery().eq(UserInfoDO::getName, name));
        return ResponseEntity.ok(userInfoService.getUserByParam(name));
    }

    @ApiOperation("根据参数获取用户")
    @GetMapping("/page")
    HttpEntity<IPage<UserInfoDO>> getUserByParam(@RequestParam("name")String name, @RequestParam("page") Integer page, @RequestParam("size") Integer size) {
///        userInfoService2.page(new Page<>(page, size));
        return ResponseEntity.ok(userInfoService.getUserPage(name, page, size));
    }

    @ApiOperation("创建用户")
    @PostMapping
    HttpEntity<?> createUser(@RequestBody UserInfoDO userInfoDO) {
///        userInfoService2.save(userInfoDO);
        userInfoService.saveUser(userInfoDO);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("更新用户")
    @PutMapping("/{id}")
    HttpEntity<?> updateUser(@PathVariable("id") Long id, @RequestBody UserInfoDO userInfoDO) {
///        userInfoService2.updateById(userInfoDO);
        userInfoService.updateUser(id, userInfoDO);
        return ResponseEntity.ok().build();
    }
}
